{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Untitled0.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "RasF2Zg66y-o"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "eqElwgPd7_fk",
        "outputId": "137b78ac-1586-44ff-8986-73a696c27cee",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 617
        }
      },
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "error",
          "ename": "KeyboardInterrupt",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
            "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m    728\u001b[0m             \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 729\u001b[0;31m                 \u001b[0mident\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreply\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdin_socket\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    730\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/jupyter_client/session.py\u001b[0m in \u001b[0;36mrecv\u001b[0;34m(self, socket, mode, content, copy)\u001b[0m\n\u001b[1;32m    802\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 803\u001b[0;31m             \u001b[0mmsg_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_multipart\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    804\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0mzmq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mZMQError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/zmq/sugar/socket.py\u001b[0m in \u001b[0;36mrecv_multipart\u001b[0;34m(self, flags, copy, track)\u001b[0m\n\u001b[1;32m    490\u001b[0m         \"\"\"\n\u001b[0;32m--> 491\u001b[0;31m         \u001b[0mparts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrack\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    492\u001b[0m         \u001b[0;31m# have first part already, only loop while more to receive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n",
            "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n",
            "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket._recv_copy\u001b[0;34m()\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/zmq/backend/cython/checkrc.pxd\u001b[0m in \u001b[0;36mzmq.backend.cython.checkrc._check_rc\u001b[0;34m()\u001b[0m\n",
            "\u001b[0;31mKeyboardInterrupt\u001b[0m: ",
            "\nDuring handling of the above exception, another exception occurred:\n",
            "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-1-d5df0069828e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mgoogle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolab\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdrive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mdrive\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmount\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'/content/drive'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/google/colab/drive.py\u001b[0m in \u001b[0;36mmount\u001b[0;34m(mountpoint, force_remount, timeout_ms, use_metadata_server)\u001b[0m\n\u001b[1;32m    249\u001b[0m       \u001b[0;32mwith\u001b[0m \u001b[0m_output\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0muse_tags\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'dfs-auth-dance'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    250\u001b[0m         \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfifo\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'w'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mfifo_file\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 251\u001b[0;31m           \u001b[0mfifo_file\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mget_code\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mauth_prompt\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'\\n'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    252\u001b[0m       \u001b[0mwrote_to_fifo\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    253\u001b[0m     \u001b[0;32melif\u001b[0m \u001b[0mcase\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m    702\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    703\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 704\u001b[0;31m             \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    705\u001b[0m         )\n\u001b[1;32m    706\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m    732\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    733\u001b[0m                 \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 734\u001b[0;31m                 \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    735\u001b[0m             \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    736\u001b[0m                 \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Y78EqqQS8OlN"
      },
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "import matplotlib.pyplot as plt"
      ],
      "execution_count": 1,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pP8Rjf6T8WiB",
        "outputId": "e2e297fd-0804-4537-acc4-3d29e93a207b",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 646
        }
      },
      "source": [
        "!wget \"http://files.grouplens.org/datasets/movielens/ml-100k.zip\"\n",
        "!unzip ml-100k.zip\n",
        "!ls"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "--2020-10-05 14:51:04--  http://files.grouplens.org/datasets/movielens/ml-100k.zip\n",
            "Resolving files.grouplens.org (files.grouplens.org)... 128.101.65.152\n",
            "Connecting to files.grouplens.org (files.grouplens.org)|128.101.65.152|:80... connected.\n",
            "HTTP request sent, awaiting response... 200 OK\n",
            "Length: 4924029 (4.7M) [application/zip]\n",
            "Saving to: ‘ml-100k.zip’\n",
            "\n",
            "ml-100k.zip         100%[===================>]   4.70M  5.60MB/s    in 0.8s    \n",
            "\n",
            "2020-10-05 14:51:05 (5.60 MB/s) - ‘ml-100k.zip’ saved [4924029/4924029]\n",
            "\n",
            "Archive:  ml-100k.zip\n",
            "   creating: ml-100k/\n",
            "  inflating: ml-100k/allbut.pl       \n",
            "  inflating: ml-100k/mku.sh          \n",
            "  inflating: ml-100k/README          \n",
            "  inflating: ml-100k/u.data          \n",
            "  inflating: ml-100k/u.genre         \n",
            "  inflating: ml-100k/u.info          \n",
            "  inflating: ml-100k/u.item          \n",
            "  inflating: ml-100k/u.occupation    \n",
            "  inflating: ml-100k/u.user          \n",
            "  inflating: ml-100k/u1.base         \n",
            "  inflating: ml-100k/u1.test         \n",
            "  inflating: ml-100k/u2.base         \n",
            "  inflating: ml-100k/u2.test         \n",
            "  inflating: ml-100k/u3.base         \n",
            "  inflating: ml-100k/u3.test         \n",
            "  inflating: ml-100k/u4.base         \n",
            "  inflating: ml-100k/u4.test         \n",
            "  inflating: ml-100k/u5.base         \n",
            "  inflating: ml-100k/u5.test         \n",
            "  inflating: ml-100k/ua.base         \n",
            "  inflating: ml-100k/ua.test         \n",
            "  inflating: ml-100k/ub.base         \n",
            "  inflating: ml-100k/ub.test         \n",
            "ml-100k  ml-100k.zip  sample_data\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tA5_lDbsPpox",
        "outputId": "89c69985-176c-4a64-cb3e-bd1929c9f264",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 323
        }
      },
      "source": [
        "!wget \"http://files.grouplens.org/datasets/movielens/ml-1m.zip\"\n",
        "!unzip ml-1m.zip\n",
        "!ls"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "--2020-10-05 14:51:19--  http://files.grouplens.org/datasets/movielens/ml-1m.zip\n",
            "Resolving files.grouplens.org (files.grouplens.org)... 128.101.65.152\n",
            "Connecting to files.grouplens.org (files.grouplens.org)|128.101.65.152|:80... connected.\n",
            "HTTP request sent, awaiting response... 200 OK\n",
            "Length: 5917549 (5.6M) [application/zip]\n",
            "Saving to: ‘ml-1m.zip’\n",
            "\n",
            "ml-1m.zip           100%[===================>]   5.64M  6.72MB/s    in 0.8s    \n",
            "\n",
            "2020-10-05 14:51:21 (6.72 MB/s) - ‘ml-1m.zip’ saved [5917549/5917549]\n",
            "\n",
            "Archive:  ml-1m.zip\n",
            "   creating: ml-1m/\n",
            "  inflating: ml-1m/movies.dat        \n",
            "  inflating: ml-1m/ratings.dat       \n",
            "  inflating: ml-1m/README            \n",
            "  inflating: ml-1m/users.dat         \n",
            "ml-100k  ml-100k.zip  ml-1m  ml-1m.zip\tsample_data\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "quPn_LWwPtdt"
      },
      "source": [
        "import torch\n",
        "import torch.nn as nn #For neural network\n",
        "import torch.nn.parallel #For parallel computation\n",
        "import torch.optim as optim #For optimizers\n",
        "import torch.utils.data \n",
        "from torch.autograd import Variable # For gradient descent"
      ],
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KIGjxK2OP6d5"
      },
      "source": [
        "# We won't be using this dataset. Just for observing\n",
        "movies = pd.read_csv('ml-1m/movies.dat', sep = '::', header = None, engine = 'python', encoding = 'latin-1')\n",
        "users = pd.read_csv('ml-1m/users.dat', sep = '::', header = None, engine = 'python', encoding = 'latin-1')\n",
        "ratings = pd.read_csv('ml-1m/ratings.dat', sep = '::', header = None, engine = 'python', encoding = 'latin-1')"
      ],
      "execution_count": 7,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GMU7s5oNQLYg"
      },
      "source": [
        "training_set = pd.read_csv('ml-100k/u1.base', delimiter = '\\t')\n",
        "training_set = np.array(training_set, dtype = 'int')\n",
        "test_set = pd.read_csv('ml-100k/u1.test', delimiter = '\\t')\n",
        "test_set = np.array(test_set, dtype = 'int')\n",
        "for i in range(2,5):\n",
        "  train_set = pd.read_csv(f'ml-100k/u{i}.base', delimiter = '\\t')\n",
        "  training_set = np.concatenate((training_set, np.array(train_set, dtype='int')),axis=0)\n",
        "  test = pd.read_csv(f'ml-100k/u{i}.test', delimiter = '\\t')\n",
        "  test_set = np.concatenate((test_set, np.array(test, dtype='int')),axis=0)"
      ],
      "execution_count": 142,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yRURCe6VQNkt",
        "outputId": "d66c2693-19da-4691-f84c-b72b21478d83",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "training_set.shape\n"
      ],
      "execution_count": 143,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(319996, 4)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 143
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "BBYFLcxTQgRk"
      },
      "source": [
        "nb_users= int(max(max(training_set[:,0]), max(test_set[:,0])))\n",
        "nb_movies= int(max(max(training_set[:,1]), max(test_set[:,1])))"
      ],
      "execution_count": 144,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qctGASxyUUfS"
      },
      "source": [
        "def convert(data):\n",
        "  new_data = []\n",
        "  for id_users in range(1, nb_users + 1):\n",
        "    id_movies = data[:, 1] [data[:, 0] == id_users]\n",
        "    id_ratings = data[:, 2] [data[:, 0] == id_users]\n",
        "    ratings = np.zeros(nb_movies)\n",
        "    ratings[id_movies - 1] = id_ratings\n",
        "    new_data.append(list(ratings))\n",
        "  return new_data\n",
        "training_set = convert(training_set)\n",
        "test_set = convert(test_set)"
      ],
      "execution_count": 145,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "K36EvHdbUuGe",
        "outputId": "59beb6c0-19a5-45f2-ec25-9f6e75fbda71",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        }
      },
      "source": [
        "training_set= torch.FloatTensor(training_set)\n",
        "test_set= torch.FloatTensor(test_set)\n",
        "training_set"
      ],
      "execution_count": 146,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0., 3., 4.,  ..., 0., 0., 0.],\n",
              "        [4., 0., 0.,  ..., 0., 0., 0.],\n",
              "        [0., 0., 0.,  ..., 0., 0., 0.],\n",
              "        ...,\n",
              "        [5., 0., 0.,  ..., 0., 0., 0.],\n",
              "        [0., 0., 0.,  ..., 0., 0., 0.],\n",
              "        [0., 5., 0.,  ..., 0., 0., 0.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 146
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XgARQfxUVsKV"
      },
      "source": [
        "#Now we build our model. Used pytorch to build this model.\n",
        "class SAE(nn.Module):\n",
        "  def __init__(self):\n",
        "    super (SAE,self).__init__()\n",
        "    self.fclayer1= nn.Linear(nb_movies,20)\n",
        "    self.fclayer2= nn.Linear(20,10)\n",
        "    self.fclayer3= nn.Linear(10,20)\n",
        "    self.fclayer4= nn.Linear(20, nb_movies)\n",
        "    self.activation= nn.Sigmoid()\n",
        "  def forward(self,x):\n",
        "    x= self.activation(self.fclayer1(x))\n",
        "    x= self.activation(self.fclayer2(x))\n",
        "    x= self.activation(self.fclayer3(x))\n",
        "    x= self.fclayer4(x)\n",
        "    return x\n"
      ],
      "execution_count": 147,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0-6Zcy05XAaL"
      },
      "source": [
        "sae= SAE()\n",
        "criterion= nn.MSELoss()\n",
        "opt= optim.RMSprop(sae.parameters(), lr=0.01, weight_decay= 0.5)"
      ],
      "execution_count": 148,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WIyf87ell3d_",
        "outputId": "6c51c769-4b64-4719-ea41-5911e31021da",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        }
      },
      "source": [
        "epochs=200\n",
        "for i in range(epochs):\n",
        "  train_loss=0\n",
        "  s=0.\n",
        "  for id_user in range(nb_users):\n",
        "    input= Variable(training_set[id_user]).unsqueeze(0)\n",
        "    target= input.clone()\n",
        "    if torch.sum(target.data>0)>0:\n",
        "      output= sae(input)\n",
        "      target.required_grad=False\n",
        "      output[target==0]=0\n",
        "      loss= criterion(output, target)\n",
        "      mean_corrector= nb_movies/float(torch.sum(target.data>0)+ 1e-10)\n",
        "      loss.backward()\n",
        "      train_loss += np.sqrt(loss.data*mean_corrector)\n",
        "      opt.step()\n",
        "      s+=1.0\n",
        "  print(f'Epoch:{i}:  Loss is {train_loss/s}')\n",
        "\n",
        "\n",
        "\n"
      ],
      "execution_count": 149,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Epoch:0:  Loss is 1.5215027332305908\n",
            "Epoch:1:  Loss is 1.0776312351226807\n",
            "Epoch:2:  Loss is 1.047215223312378\n",
            "Epoch:3:  Loss is 1.035642385482788\n",
            "Epoch:4:  Loss is 1.0302492380142212\n",
            "Epoch:5:  Loss is 1.0264378786087036\n",
            "Epoch:6:  Loss is 1.0247740745544434\n",
            "Epoch:7:  Loss is 1.0230616331100464\n",
            "Epoch:8:  Loss is 1.0222581624984741\n",
            "Epoch:9:  Loss is 1.0215506553649902\n",
            "Epoch:10:  Loss is 1.0210528373718262\n",
            "Epoch:11:  Loss is 1.0203781127929688\n",
            "Epoch:12:  Loss is 1.020032525062561\n",
            "Epoch:13:  Loss is 1.0199869871139526\n",
            "Epoch:14:  Loss is 1.0195577144622803\n",
            "Epoch:15:  Loss is 1.0192489624023438\n",
            "Epoch:16:  Loss is 1.0193008184432983\n",
            "Epoch:17:  Loss is 1.0190917253494263\n",
            "Epoch:18:  Loss is 1.0183618068695068\n",
            "Epoch:19:  Loss is 1.0172207355499268\n",
            "Epoch:20:  Loss is 1.015598177909851\n",
            "Epoch:21:  Loss is 1.0150617361068726\n",
            "Epoch:22:  Loss is 1.0119060277938843\n",
            "Epoch:23:  Loss is 1.0107215642929077\n",
            "Epoch:24:  Loss is 1.0073063373565674\n",
            "Epoch:25:  Loss is 1.0044883489608765\n",
            "Epoch:26:  Loss is 1.0007716417312622\n",
            "Epoch:27:  Loss is 0.9959368705749512\n",
            "Epoch:28:  Loss is 0.9934889078140259\n",
            "Epoch:29:  Loss is 0.9925919771194458\n",
            "Epoch:30:  Loss is 0.9881447553634644\n",
            "Epoch:31:  Loss is 0.9952220320701599\n",
            "Epoch:32:  Loss is 0.9951487183570862\n",
            "Epoch:33:  Loss is 0.9919514656066895\n",
            "Epoch:34:  Loss is 0.9913524389266968\n",
            "Epoch:35:  Loss is 0.9930900931358337\n",
            "Epoch:36:  Loss is 0.9886407852172852\n",
            "Epoch:37:  Loss is 0.9899955987930298\n",
            "Epoch:38:  Loss is 0.9904050827026367\n",
            "Epoch:39:  Loss is 0.9889170527458191\n",
            "Epoch:40:  Loss is 0.9854351282119751\n",
            "Epoch:41:  Loss is 0.9841718673706055\n",
            "Epoch:42:  Loss is 0.9809065461158752\n",
            "Epoch:43:  Loss is 0.9838259816169739\n",
            "Epoch:44:  Loss is 0.9841470122337341\n",
            "Epoch:45:  Loss is 0.9942716956138611\n",
            "Epoch:46:  Loss is 0.9944350123405457\n",
            "Epoch:47:  Loss is 0.9890116453170776\n",
            "Epoch:48:  Loss is 0.9836329221725464\n",
            "Epoch:49:  Loss is 0.9827690124511719\n",
            "Epoch:50:  Loss is 0.9793740510940552\n",
            "Epoch:51:  Loss is 0.9790423512458801\n",
            "Epoch:52:  Loss is 0.9739201664924622\n",
            "Epoch:53:  Loss is 0.972234308719635\n",
            "Epoch:54:  Loss is 0.9782992005348206\n",
            "Epoch:55:  Loss is 0.9855266213417053\n",
            "Epoch:56:  Loss is 0.9772093296051025\n",
            "Epoch:57:  Loss is 0.9777383208274841\n",
            "Epoch:58:  Loss is 0.9755076766014099\n",
            "Epoch:59:  Loss is 0.9765203595161438\n",
            "Epoch:60:  Loss is 0.9744977355003357\n",
            "Epoch:61:  Loss is 0.9817421436309814\n",
            "Epoch:62:  Loss is 0.9739622473716736\n",
            "Epoch:63:  Loss is 0.9714165329933167\n",
            "Epoch:64:  Loss is 0.9666439294815063\n",
            "Epoch:65:  Loss is 0.9672999978065491\n",
            "Epoch:66:  Loss is 0.9628874063491821\n",
            "Epoch:67:  Loss is 0.9616640210151672\n",
            "Epoch:68:  Loss is 0.9611112475395203\n",
            "Epoch:69:  Loss is 0.9571037888526917\n",
            "Epoch:70:  Loss is 0.955640435218811\n",
            "Epoch:71:  Loss is 0.9550722241401672\n",
            "Epoch:72:  Loss is 0.9546109437942505\n",
            "Epoch:73:  Loss is 0.9525454640388489\n",
            "Epoch:74:  Loss is 0.9523218870162964\n",
            "Epoch:75:  Loss is 0.9518213272094727\n",
            "Epoch:76:  Loss is 0.9512323141098022\n",
            "Epoch:77:  Loss is 0.9564543962478638\n",
            "Epoch:78:  Loss is 0.9551262259483337\n",
            "Epoch:79:  Loss is 0.9532551169395447\n",
            "Epoch:80:  Loss is 0.9605816602706909\n",
            "Epoch:81:  Loss is 0.9616262912750244\n",
            "Epoch:82:  Loss is 0.9567480087280273\n",
            "Epoch:83:  Loss is 0.9552135467529297\n",
            "Epoch:84:  Loss is 0.9534356594085693\n",
            "Epoch:85:  Loss is 0.95188969373703\n",
            "Epoch:86:  Loss is 0.9505441188812256\n",
            "Epoch:87:  Loss is 0.949798047542572\n",
            "Epoch:88:  Loss is 0.949287474155426\n",
            "Epoch:89:  Loss is 0.9488305449485779\n",
            "Epoch:90:  Loss is 0.9482321739196777\n",
            "Epoch:91:  Loss is 0.9483969211578369\n",
            "Epoch:92:  Loss is 0.9503089189529419\n",
            "Epoch:93:  Loss is 0.947920024394989\n",
            "Epoch:94:  Loss is 0.9463355541229248\n",
            "Epoch:95:  Loss is 0.9453402757644653\n",
            "Epoch:96:  Loss is 0.9451221823692322\n",
            "Epoch:97:  Loss is 0.9440867900848389\n",
            "Epoch:98:  Loss is 0.9435286521911621\n",
            "Epoch:99:  Loss is 0.9432835578918457\n",
            "Epoch:100:  Loss is 0.9483363032341003\n",
            "Epoch:101:  Loss is 0.9421319961547852\n",
            "Epoch:102:  Loss is 0.9407809376716614\n",
            "Epoch:103:  Loss is 0.9403315186500549\n",
            "Epoch:104:  Loss is 0.9394916296005249\n",
            "Epoch:105:  Loss is 0.9392949342727661\n",
            "Epoch:106:  Loss is 0.939170241355896\n",
            "Epoch:107:  Loss is 0.9386966228485107\n",
            "Epoch:108:  Loss is 0.9382123351097107\n",
            "Epoch:109:  Loss is 0.937436044216156\n",
            "Epoch:110:  Loss is 0.9370661973953247\n",
            "Epoch:111:  Loss is 0.9365803599357605\n",
            "Epoch:112:  Loss is 0.9364107251167297\n",
            "Epoch:113:  Loss is 0.93650221824646\n",
            "Epoch:114:  Loss is 0.9360711574554443\n",
            "Epoch:115:  Loss is 0.9359935522079468\n",
            "Epoch:116:  Loss is 0.9351990222930908\n",
            "Epoch:117:  Loss is 0.9348465800285339\n",
            "Epoch:118:  Loss is 0.9345989227294922\n",
            "Epoch:119:  Loss is 0.9344156980514526\n",
            "Epoch:120:  Loss is 0.9340527653694153\n",
            "Epoch:121:  Loss is 0.9338953495025635\n",
            "Epoch:122:  Loss is 0.9332727193832397\n",
            "Epoch:123:  Loss is 0.93332839012146\n",
            "Epoch:124:  Loss is 0.9328713417053223\n",
            "Epoch:125:  Loss is 0.9323346614837646\n",
            "Epoch:126:  Loss is 0.9320321679115295\n",
            "Epoch:127:  Loss is 0.9318073987960815\n",
            "Epoch:128:  Loss is 0.9315369129180908\n",
            "Epoch:129:  Loss is 0.9318140745162964\n",
            "Epoch:130:  Loss is 0.9313086271286011\n",
            "Epoch:131:  Loss is 0.9314699172973633\n",
            "Epoch:132:  Loss is 0.9310522079467773\n",
            "Epoch:133:  Loss is 0.9310598373413086\n",
            "Epoch:134:  Loss is 0.9308615922927856\n",
            "Epoch:135:  Loss is 0.9304450750350952\n",
            "Epoch:136:  Loss is 0.9303900599479675\n",
            "Epoch:137:  Loss is 0.9301888942718506\n",
            "Epoch:138:  Loss is 0.929746150970459\n",
            "Epoch:139:  Loss is 0.9296042919158936\n",
            "Epoch:140:  Loss is 0.9294366240501404\n",
            "Epoch:141:  Loss is 0.9294692873954773\n",
            "Epoch:142:  Loss is 0.9290640354156494\n",
            "Epoch:143:  Loss is 0.9288455247879028\n",
            "Epoch:144:  Loss is 0.9287498593330383\n",
            "Epoch:145:  Loss is 0.9282993674278259\n",
            "Epoch:146:  Loss is 0.9297746419906616\n",
            "Epoch:147:  Loss is 0.9282055497169495\n",
            "Epoch:148:  Loss is 0.9274322390556335\n",
            "Epoch:149:  Loss is 0.9281322360038757\n",
            "Epoch:150:  Loss is 0.9312627911567688\n",
            "Epoch:151:  Loss is 0.9296138286590576\n",
            "Epoch:152:  Loss is 0.9276072382926941\n",
            "Epoch:153:  Loss is 0.9277066588401794\n",
            "Epoch:154:  Loss is 0.9265255928039551\n",
            "Epoch:155:  Loss is 0.9267794489860535\n",
            "Epoch:156:  Loss is 0.9289935827255249\n",
            "Epoch:157:  Loss is 0.926200270652771\n",
            "Epoch:158:  Loss is 0.9264520406723022\n",
            "Epoch:159:  Loss is 0.9285306930541992\n",
            "Epoch:160:  Loss is 0.9277082085609436\n",
            "Epoch:161:  Loss is 0.9277059435844421\n",
            "Epoch:162:  Loss is 0.9279720783233643\n",
            "Epoch:163:  Loss is 0.927876353263855\n",
            "Epoch:164:  Loss is 0.9258730411529541\n",
            "Epoch:165:  Loss is 0.9267586469650269\n",
            "Epoch:166:  Loss is 0.9240193963050842\n",
            "Epoch:167:  Loss is 0.9229799509048462\n",
            "Epoch:168:  Loss is 0.9219385981559753\n",
            "Epoch:169:  Loss is 0.9220580458641052\n",
            "Epoch:170:  Loss is 0.9213890433311462\n",
            "Epoch:171:  Loss is 0.9216106534004211\n",
            "Epoch:172:  Loss is 0.9203181266784668\n",
            "Epoch:173:  Loss is 0.9208689332008362\n",
            "Epoch:174:  Loss is 0.9232712984085083\n",
            "Epoch:175:  Loss is 0.9211623072624207\n",
            "Epoch:176:  Loss is 0.9212639927864075\n",
            "Epoch:177:  Loss is 0.9193546772003174\n",
            "Epoch:178:  Loss is 0.9186646938323975\n",
            "Epoch:179:  Loss is 0.9189736843109131\n",
            "Epoch:180:  Loss is 0.9175172448158264\n",
            "Epoch:181:  Loss is 0.9190583229064941\n",
            "Epoch:182:  Loss is 0.9168023467063904\n",
            "Epoch:183:  Loss is 0.917508602142334\n",
            "Epoch:184:  Loss is 0.9159185290336609\n",
            "Epoch:185:  Loss is 0.9163827896118164\n",
            "Epoch:186:  Loss is 0.9146879315376282\n",
            "Epoch:187:  Loss is 0.914949357509613\n",
            "Epoch:188:  Loss is 0.9142611026763916\n",
            "Epoch:189:  Loss is 0.9135792255401611\n",
            "Epoch:190:  Loss is 0.9131029844284058\n",
            "Epoch:191:  Loss is 0.9125852584838867\n",
            "Epoch:192:  Loss is 0.9117634892463684\n",
            "Epoch:193:  Loss is 0.911829948425293\n",
            "Epoch:194:  Loss is 0.9106287360191345\n",
            "Epoch:195:  Loss is 0.9103925824165344\n",
            "Epoch:196:  Loss is 0.9094666838645935\n",
            "Epoch:197:  Loss is 0.9097420573234558\n",
            "Epoch:198:  Loss is 0.9086841344833374\n",
            "Epoch:199:  Loss is 0.9087303280830383\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SQzwqKGPz39r",
        "outputId": "98aeed67-b94d-4bd9-937a-c3f1a33dc2bc",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        " test_loss=0\n",
        "s=0.\n",
        "for id_user in range(nb_users):\n",
        "  input= Variable(training_set[id_user]).unsqueeze(0)\n",
        "  target= Variable(test_set[id_user]).unsqueeze(0)\n",
        "  if torch.sum(target.data>0)>0:\n",
        "    s+=1.\n",
        "    output= sae(input)\n",
        "    target.required_grad=False\n",
        "    output[target==0]=0\n",
        "    loss= criterion(output, target)\n",
        "    mean_corrector= nb_movies/float(torch.sum(target.data>0)+ 1e-10)\n",
        "    \n",
        "    test_loss += np.sqrt(loss.data*mean_corrector)\n",
        "print(test_loss/s)\n",
        "      "
      ],
      "execution_count": 150,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor(0.9114)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gqUM_5p80BwD"
      },
      "source": [
        ""
      ],
      "execution_count": 80,
      "outputs": []
    }
  ]
}